from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import base64
import time


class AESCrypto_Yzdyh:
    def __init__(self, default_key="KrQ4KAYOEyAz66RS", default_iv="ep1YCmxXuxKe4eH1"):
        """
        :param default_key: 默认密钥，16/24/32字节
        :param default_iv: 默认IV，16字节
        """
        self.default_key = default_key.encode('utf-8')
        self.default_iv = default_iv.encode('utf-8')

    def encrypt(self, plaintext, key=None, iv=None):
        """
        AES-CBC加密 (PKCS7填充)
        :return: Base64编码的密文
        """
        key = key.encode('utf-8') if key else self.default_key
        iv = iv.encode('utf-8') if iv else self.default_iv

        cipher = AES.new(key, AES.MODE_CBC, iv)
        padded_data = pad(plaintext.encode('utf-8'), AES.block_size)
        ciphertext = cipher.encrypt(padded_data)
        return base64.b64encode(ciphertext).decode('utf-8')

    def decrypt(self, ciphertext, key=None, iv=None):
        """
        AES-CBC解密 (PKCS7填充)
        :return: 原始明文
        """
        key = key.encode('utf-8') if key else self.default_key
        iv = iv.encode('utf-8') if iv else self.default_iv

        cipher = AES.new(key, AES.MODE_CBC, iv)
        decrypted = cipher.decrypt(base64.b64decode(ciphertext))
        return unpad(decrypted, AES.block_size).decode('utf-8')


    def getptSign(self):
        timestamp = int(time.time() * 1000)
        signStr = str(timestamp) + "@oZiV06-dCtvKL5Xv3LOF6FoKdCTg"
        return  self.encrypt(signStr)

    def getDdSign(self):
        timestamp = int(time.time() * 1000)
        signStr = str(timestamp) + "@oZiV06-dCtvKL5Xv3LOF6FoKdCTg@booking"
        return  self.encrypt(signStr)
